#include "wrap.h"

//处理错误函数
void perr_exit(const char *s)
{
   perror(s);
   exit(ERRNUM);
}


//绑定函数
int Bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen)
{
    int n = bind(sockfd,addr,addrlen);
    if(n == ERRNUM)
    {
        perr_exit("bind fail\n");
        close(sockfd);
    }
    return n;
}


//监听函数
int Listen(int sockfd, int backlog)
{
    int n = listen(sockfd,backlog);
    if(n == ERRNUM)
    {
        perr_exit("listen fail\n");
    }
    return n;
}


//链接函数客户端链接服务器
int Connect(int sockfd,const struct sockaddr *addr,socklen_t addrlen)
{
    int n = connect(sockfd,addr,addrlen);
    if(n == ERRNUM)
    {
        perr_exit("connect fail\n");
        close(sockfd);
    }
    return n;
}

int Socket(int domain, int type, int protocol)
{
   int n = socket(domain,type,protocol);
   if(n == ERRNUM)
   {
      perr_exit("socket fail\n");
   }
   return n;

}

int Epoll_create(int size)
{
     int n = epoll_create(1);
     if(n == ERRNUM)
     {
        perr_exit("epoll_create fail\n");
     }
     return n;
}

int Epoll_ctl(int epfd,int op,int fd,struct epoll_event *event)
{
     int n = epoll_ctl(epfd,op,fd,event);
     if(n == ERRNUM)
     {
        perr_exit("epoll_ctl fail\n");
     }
     return n;
}


//阻塞，events中存放阻塞事件
int Epoll_wait(int epfd,struct epoll_event *events,int maxevents,int timeout)
{
     int n = epoll_wait(epfd,events,maxevents,timeout);
     if(n == ERRNUM)
     {
        perr_exit("epoll_wait fail\n");
     }
     return n;
}


